package com.controller.system;

import com.common.core.controller.BaseController;
import com.common.utils.AjaxResult;
import com.common.utils.PageUtils;
import com.system.entity.SysRoleEntity;
import com.system.service.ISysRoleService;
import com.system.service.SystemMenuService;
import com.vo.UserVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 角色信息表
 *
 * @author yangzhan
 * @email *****@mail.com
 * @date 2022-04-11 20:41:25
 */
@Controller
@RequestMapping("/sysRole")
public class SysRoleController extends BaseController {

    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private SystemMenuService systemMenuService;

    /**
     * 跳转到页面
     */
    @GetMapping("/list")
    @RequiresPermissions("system:role:view")
    public String sysRole() {
        return "system/sysrole/list" ;
    }

    /**
     * 角色信息表 添加
     *
     * @param sysRoleEntity
     */
    @PostMapping("/add")
    @RequiresPermissions("system:role:add")
    @ResponseBody
    public AjaxResult add(UserVO userVO, @RequestBody SysRoleEntity sysRoleEntity) {
        if (sysRoleEntity.getStatus() == null) {
            sysRoleEntity.setStatus("1");
        }
        sysRoleEntity.setCreateBy(userVO.getLoginName());
        return sysRoleService.sysRoleAdd(sysRoleEntity);
    }

    /**
     * 角色信息表
     *
     * @param ids
     */
    @DeleteMapping("/delete")
    @RequiresPermissions("system:role:delete")
    @ResponseBody
    public AjaxResult delete(@RequestBody List<Integer> ids) {
        return sysRoleService.sysRoleDel(ids);
    }

    /**
     * @param sysRoleEntity 更新数据
     */
    @PutMapping("/update")
    @RequiresPermissions("system:role:update")
    @ResponseBody
    public AjaxResult update(UserVO userVO, @RequestBody SysRoleEntity sysRoleEntity) {
        if (sysRoleEntity.getStatus() == null) {
            sysRoleEntity.setStatus("1");
        }
        sysRoleEntity.setCreateBy(userVO.getLoginName());
        return sysRoleService.sysRoleUpById(sysRoleEntity);
    }

    /**
     * @param sysRoleEntity 数据查询
     */
    @PostMapping("/listByPage")
    @RequiresPermissions("system:role:list")
    @ResponseBody
    public AjaxResult findListByPage(@RequestBody SysRoleEntity sysRoleEntity) {
        sysRoleEntity.setPage(PageUtils.pageUtils(sysRoleEntity.getPage(), sysRoleEntity.getLimit()));
        return sysRoleService.sysRoleSelectListByPage(sysRoleEntity);
    }

    /**
     * 修改角色管理角色的启用与停用
     *
     * @param status
     * @return
     */
    @GetMapping("/upSta")
    @ResponseBody
    public AjaxResult upStatus(@RequestParam("status") Integer status) {
        return sysRoleService.upStatus(status);
    }


    /**
     * 添加角色 菜单表
     *
     * @return
     */
    @GetMapping("/data")
    @ResponseBody
    public Map<String, Object> dataAdd() {
        Map<String, Object> t = systemMenuService.getTree();
        return t;
    }

    /**
     * 修改角色菜单表
     *
     * @return
     */
    @GetMapping("/data1")
    @ResponseBody
    public Map<String, Object> dataUp(@RequestParam("id") Long id) {
        Map<String, Object> t = systemMenuService.getTreeUp(id);
        return t;
    }


}
